/*
 *    Copyright (c) 2018-2025, 云集汇通 All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the yunjihuitong.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 */

package com.yjht.modules.sys.controller;

import com.yjht.common.constant.Constant;
import com.yjht.common.utils.PageUtils;
import com.yjht.common.utils.R;
import com.yjht.common.func.validator.ValidatorUtils;
import com.yjht.modules.sys.entity.SysDictEntity;
import com.yjht.modules.sys.service.SysDictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典
 *
 * @author Mark wangiegie@gmail.com
 * @since 3.1.0 2018-01-27
 */
@RestController
@RequestMapping("sys/dict")
public class SysDictController {
    @Autowired
    private SysDictService sysDictService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:dict:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysDictService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:dict:info")
    public R info(@PathVariable("id") Long id){
        SysDictEntity dict = sysDictService.selectById(id);

        return R.ok().put("dict", dict);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:dict:save")
    public R save(@RequestBody SysDictEntity dict){
        //校验类型
        ValidatorUtils.validateEntity(dict);

        sysDictService.insert(dict);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:dict:update")
    public R update(@RequestBody SysDictEntity dict){
        //校验类型
        ValidatorUtils.validateEntity(dict);

        sysDictService.updateById(dict);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:dict:delete")
    public R delete(@RequestBody Long[] ids){
        sysDictService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }
    /**
     * 一级字典目录
     */
    @RequestMapping("/firstDir")
    @RequiresPermissions("sys:dict:info")
    public R firstDirec(){
        int parentId = Constant.top_dict_id;
        Map<String,Object> map = new HashMap<>();
        map.put("parentid",0);
        List<SysDictEntity> list = sysDictService.selectByMap(map);
        SysDictEntity entity = new SysDictEntity();
        entity.setId(0L);
        entity.setName("总父类");
        list.add(entity);
        return R.ok().put("list", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/type/{type}")
    @RequiresPermissions("sys:dict:info")
    public List<SysDictEntity> infoList(@PathVariable("type") String type){
        //测试条件
        type = "themes";
        SysDictEntity dict = new SysDictEntity();
        dict.setType(type);
        dict.setParentid(0L);
        List<SysDictEntity> list = sysDictService.queryList(dict);

        return list;
    }
}
